//
//  PKRadioDetailViewController.m
//  PianKe
//
//  Created by lanou on 15/10/5.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//

#import "PKRadioDetailViewController.h"
#import "PKRadioDetailCell.h"
#import "PKRequestManager.h"
#import "PKRadioDetailCell.h"
#import "PKPlayViewController.h"
#import "PKRadioDetailHeadView.h"
@interface PKRadioDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) PKRadioDetailHeadView *headView;
@property (nonatomic, strong) NSMutableDictionary *radioInfoDict;
@end

@implementation PKRadioDetailViewController
-(NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray)
    {
        self.dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WITDTH, SCREEN_HEIGHT - 50)];
    _mainTableView.rowHeight = 120;
    _mainTableView.dataSource = self;
    _mainTableView.delegate = self;
    [_mainTableView registerClass:[PKRadioDetailCell class] forCellReuseIdentifier:@"PKRadioDetailCell"];
    
    _headView = [[PKRadioDetailHeadView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WITDTH, SCREEN_HEIGHT/5 + 65)];
    _mainTableView.tableHeaderView = _headView;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTableView];
    
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(12, 18, 20, 20);
    backButton.backgroundColor = [UIColor grayColor];
    [backButton setImage:[UIImage imageNamed:@"后退"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToMainListView) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:backButton];
  
    [self requestData];
    
    
    
}
-(void)backToMainListView
{
    
    [self.navigationController popViewControllerAnimated:YES ];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKRadioDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKRadioDetailCell" forIndexPath:indexPath];
    cell.configureModel = self.dataSourceArray[indexPath.row];
    return cell;
}

- (void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"radioid"] = _radioid;
    parDic[@"version"] = @"3.0.4";
    
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/ting/radio_detail" parDic:parDic finish:^(NSData *data)
     {
        
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSArray *listArray = dic[@"data"][@"list"];
         for (NSDictionary *modelDic in listArray)
         {
             PKRadioDetailModel *model = [[PKRadioDetailModel alloc] init];
             [model setValuesForKeysWithDictionary:modelDic];
             [_dataSourceArray addObject:model];
         }
         
         self.radioInfoDict = dic[@"data"][@"radioInfo"];
         [_headView configureDetailModel:self.radioInfoDict];
 
         [_mainTableView reloadData];
     } error:^(NSError *error)
     {
         NSLog(@"电台主页面网络请求失败%@", error);
     }];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  [PKRadioDetailCell heightForCell];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKPlayViewController *playVC = [[PKPlayViewController alloc]init];
    
    for (int i = 0; i <_dataSourceArray.count; i++) {
        PKRadioDetailModel *model = _dataSourceArray[i];
        if (i== indexPath.row) {
            model.isSelected = YES;
        }else{
            model.isSelected = NO;
        }
    }
    
    playVC.dataSourceArray = self.dataSourceArray;
    playVC.index = indexPath.row;
//    playVC.titleLabel.text = [self.dataSourceArray[indexPath.row] title];
    
    
    [self.navigationController pushViewController:playVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
